/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdBountyHunter;

import com.SySammy.SdBountyHunter.Handlers.BountyHandler;
import com.SySammy.SdBountyHunter.Listeners.BListener;
import com.SySammy.SdBountyHunter.Listeners.EListener;
import com.SySammy.SdBountyHunter.Listeners.PListener;
import com.SySammy.SdBountyHunter.Prop.BountyHunterProperties;
import com.SySammy.SdBountyHunter.commands.sdBountyHunterCmds;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.persistence.PersistenceException;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

/**
 *
 * @author Sammy
 */
public class sdLaw extends JavaPlugin {

    private static final Logger log = Logger.getLogger("Minecraft");
    private static final String currencyDIR = "plugins/RPGmod/bountyhunter/";
    private final EListener EL = new EListener(this);
    private final BListener BL = new BListener(this);
    private final PListener PL = new PListener(this);
    public PermissionHandler Permissions;
    private boolean UsePermissions;

    public static void main(String[] args) {
        // TODO Debug
    }

    @Override
    public void onDisable() {
        System.out.println("disabled, goodbye wold");
    }

    @Override
    public void onEnable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        PluginManager pm = getServer().getPluginManager();
        new File(currencyDIR).mkdirs();
        BountyHunterProperties.loadCurrencyProps();
        setupPermissions();
        setupDatabase();
        getCommand("bounty").setExecutor(new sdBountyHunterCmds(this));
        getCommand("prison").setExecutor(new sdBountyHunterCmds(this));
        pm.registerEvent(Event.Type.ENTITY_DAMAGE, this.EL, Event.Priority.Highest, this);
        pm.registerEvent(Event.Type.BLOCK_BREAK, this.BL, Event.Priority.Highest, this);
        pm.registerEvent(Event.Type.PLAYER_INTERACT, this.PL, Event.Priority.Highest, this);
        pm.registerEvent(Event.Type.PLAYER_TELEPORT, this.PL, Event.Priority.Highest, this);
        System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!");
        BountyHandler BH = new BountyHandler(this);
        BH.RemoveaAllCaughts();
    }

    private void setupDatabase() {
        try {
            getDatabase().find(PersistenceDataBase.class).findRowCount();
        } catch (PersistenceException ex) {
            System.out.println("Installing database for " + getDescription().getName() + " due to first time usage");
            installDDL();
        }
    }

    @Override
    public List<Class<?>> getDatabaseClasses() {
        List<Class<?>> list = new ArrayList<Class<?>>();
        list.add(PersistenceDataBase.class);
        list.add(PrisonDataBase.class);
        return list;
    }

    private void setupPermissions() {
        Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");

        if (this.Permissions == null) {
            if (test != null) {
                UsePermissions = true;
                this.Permissions = ((Permissions) test).getHandler();
                System.out.println("Permissions is enable for sdBountyHunter.");
            } else {
                log.info("Permission system not detected, defaulting to OP");
                UsePermissions = false;
            }
        }
    }

    public boolean casUseBountylist(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bounty.list");
        }
        return p.isOp();
    }

    public boolean casUseBountySet(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bounty.set");
        }
        return p.isOp();
    }

    public boolean casUseBountyRemove(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bounty.remove");
        }
        return p.isOp();
    }

    public boolean casUseBountyPrison(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bounty.prison.*");
        }
        return p.isOp();
    }

    public boolean casUseBountyPrisonMake(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bounty.prison.make");
        }
        return p.isOp();
    }

    public boolean casUseBountyPrisonRemove(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bounty.prison.remove");
        }
        return p.isOp();
    }

    public boolean casUseBountyHelp(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bounty.help");
        }
        return p.isOp();
    }
}
